package ssm.mapper;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ssm.pojo.User;

public class UserMapperTest {

//    private SqlSessionFactory sqlSessionFactory;
////    @Before
////    public void setUp() throws Exception{
////        // mybatis配置文件
////        String resource = "mybatis/SqlMapConfig.xml";
////        // 得到配置文件流
////        InputStream inputStream = Resources.getResourceAsStream(resource);
////
////        // 创建会话工厂，传入mybatis的配置文件信息
////        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
////
////
////    }
////
////    @Test
////    public void testFindUserById() throws Exception {
////
////        SqlSession sqlSession = sqlSessionFactory.openSession();
////
////        //创建UserMapper对象，mybatis自动生成mapper代理对象
////        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
////
////        //调用userMapper方法
////        User user = userMapper.findUserById(1);
////
////        System.out.println(user.getUsername());
////
////
////    }
////
////    @Test
////    public void testfindUserByIdResultMap() throws Exception {
////
////        SqlSession sqlSession = sqlSessionFactory.openSession();
////
////        //创建UserMapper对象，mybatis自动生成mapper代理对象
////        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
////
////        //调用userMapper方法
////        User user = userMapper.findUserByIdResultMap(1);
////
////        System.out.println(user.getUsername());
////        System.out.println(user.getId());
////
////
////    }
////
////    @Test
////    public void testFindUserList()throws Exception{
////
////        SqlSession sqlSession = sqlSessionFactory.openSession();
////
////        //创建UserMapper对象，mybatis自动生成mapper代理对象
////        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
////
////        //创建包装对象，设置查询条件
////        UserQueryVo userQueryVo = new UserQueryVo();
////        UserCustom userCustom = new UserCustom();
//////        userCustom.setSex("1");
////        userCustom.setUsername("张");
////        userQueryVo.setUserCustom(userCustom);
////
////        List<UserCustom> list = userMapper.findUserList(userQueryVo);
////
////        System.out.println(list.size());
////
////    }
////
////    @Test
////    public void testFindUserCount()throws Exception{
////
////        SqlSession sqlSession = sqlSessionFactory.openSession();
////
////        //创建UserMapper对象，mybatis自动生成mapper代理对象
////        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
////
////        //创建包装对象，设置查询条件
////        UserQueryVo userQueryVo = new UserQueryVo();
////        UserCustom userCustom = new UserCustom();
////        userCustom.setSex("1");
////        userCustom.setUsername("张");
////
////        List<Integer> list = new ArrayList<Integer>();
////        list.add(1);
////        list.add(2);
////        list.add(3);
////        list.add(4);
////
////
////        userQueryVo.setIds(list);
////        userQueryVo.setUserCustom(userCustom);
////
////        int Count = userMapper.findUserCount(userQueryVo);
////
////        System.out.println(Count);
////
////    }
////
////
////
////    @Test
////    public void testFindUserByName() throws Exception {
////
////        SqlSession sqlSession = sqlSessionFactory.openSession();
////
////        //创建UserMapper对象，mybatis自动生成mapper代理对象
////        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
////
////        //调用userMapper方法
////        List<User> list = userMapper.findUserByName("小明");
////
////        sqlSession.close();
////
////        System.out.println(list.size());
////
////    }

    private ApplicationContext applicationContext;
    @Before
    public void setUp() throws Exception{
//        // mybatis配置文件
//        String resource = "mybatis/SqlMapConfig.xml";
//        // 得到配置文件流
//        InputStream inputStream = Resources.getResourceAsStream(resource);
//
//        // 创建会话工厂，传入mybatis的配置文件信息
//        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");




    }

    @Test
    public void testFindUserById() throws Exception {

//        //创建UserDao对象
//        UserDao userDao = new UserDaoImpl(sqlSessionFactory);
        UserMapper userMapper = applicationContext.getBean(UserMapper.class);
        //调用UserDao方法
        User user = userMapper.findUserById(1);

        System.out.println(user.getUsername());

    }




}
